{extend name="admin@index_layout"/}
{block name="main"}
<style type="text/css">
.childrenBody {
    background: #fff;
}
</style>
<div class="layui-form">
        <blockquote class="layui-elem-quote quoteBox">
            <form class="layui-form searchForm" id="searchForm" method="get">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="filter_time_range" class="layui-input w200" id="laydate" placeholder="搜索时间范围">
                    </div>
                    <div class="layui-input-inline">
                        <input type="text" name="keyword" class="layui-input" placeholder="请输入标题关键词">
                    </div>
                    <a class="layui-btn search_btn" data-type="reload" onclick="searchForm()">搜索</a>
                    <a class="layui-btn layui-btn-normal" onclick="resetForm()"><i class="layui-icon layui-icon-refresh-1"></i> 重置</a>
                </div>
            </form>
        </blockquote>
    <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
</div>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
      <a class="layui-btn layui-btn-sm" data-open="{:url('add',['catid'=>$catid])}" data-title="新增内容" data-full="true">新增内容</a>
      <a class="layui-btn layui-btn-sm layui-btn-danger confirm layui-batch-all" data-href='{:url("delete",["catid"=>$catid])}'>批量删除</a>
      <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="move">批量移动</a>
      <a class="layui-btn layui-btn-sm layui-btn-danger" href="{:url('recycle',['catid'=>$catid])}">回收站</a>
    </div>
</script>
<script type="text/html" id="barTool">
    <a class="layui-btn layui-btn-xs" data-open='{:url("edit",["catid"=>$catid])}?id={{ d.id }}' data-title="编辑内容" data-full="true">编辑</a>
    <a href='{:url("delete",["catid"=>$catid])}?ids={{ d.id }}' class="layui-btn layui-btn-danger layui-btn-xs layui-tr-del">删除</a>
</script>
<script type="text/html" id="title">
  {{# if(d.flag.indexOf("1")!==-1){ }}
  <span class="text-danger">[置顶]</span>
  {{#  } }}
  {{# if(d.flag.indexOf("2")!==-1){ }}
  <span class="text-danger">[头条]</span>
  {{#  } }}
  {{# if(d.flag.indexOf("3")!==-1){ }}
  <span class="text-danger">[特荐]</span>
  {{#  } }}
  {{# if(d.flag.indexOf("4")!==-1){ }}
  <span class="text-danger">[推荐]</span>
  {{#  } }}
  {{# if(d.flag.indexOf("5")!==-1){ }}
  <span class="text-danger">[热点]</span>
  {{#  } }}
  {{# if(d.flag.indexOf("6")!==-1){ }}
  <span class="text-danger">[幻灯]</span>
  {{#  } }}
  {{# if(d.thumb!==0){ }}
  <span class="text-success">[有图]</span>
  {{#  } }}
  {{ d.title }}
</script>
<script type="text/html" id="username">
	{{# if(d.sysadd==1){ }}
	{{ d.username }}
	{{#  } else { }}
	<span class="text-danger">{{ d.username }}</span>
	{{#  } }}
</script>
<script type="text/html" id="statusTpl">
    <input type="checkbox" name="status" data-href='{:url("setstate",["catid"=>$catid])}?id={{d.id}}' value="{{d.id}}" lay-skin="switch" lay-text="通过|待审核" lay-filter="switchStatus" {{ d.status==1 ? 'checked' : '' }}>
</script>
<div id="remove" style="display: none;">
	<div class="box-body" style="padding: 20px;">
		<blockquote class="layui-elem-quote">只能将数据移动到相同模型的栏目下，不同模型的数据移动将被忽略</blockquote>
		<div class="layui-form">
		    <div class="layui-form-item">
	            <select name="remove" lay-verify="required">
	                <option></option>
	                {$string|raw}
	            </select>
		    </div>
		</div>
	</div>
</div>
<style type="text/css">
.layui-layer-page .layui-layer-content {
    overflow: inherit;
}
</style>
{/block}
{block name="script"}
<script>
    var options = {
        id: 'dataTable',
        formId: 'searchForm',
        toolbar: '#toolbarDemo',
        url: '{:url("cms/cms/classlist",["catid"=>$catid])}',
        page: true,   //分页设置
        sortOrder: '', //排序方式  asc: 升序、desc: 降序、null: 默认排序
        cols: [
            [
                { type: 'checkbox', fixed: 'left' },
                { field: 'listorder', width: 70, title: '排序', edit: 'text' },
                { field: 'id', width: 60, title: 'ID' },
                { field: 'title', title: '标题',templet: '#title'},
                { field: 'hits', width: 80, title: '点击量' },
                { field: 'updatetime', width: 160, title: '更新时间' },
                { field: 'username', width: 80, title: '发布人',templet: '#username' },
                { field: 'url',width: 60,align:"center", title: 'URL',templet:'<div><a href="{{ d.url }}" target="_blank"><i class="iconfont icon-lianjie"></i></a></div>'},
                { field: 'status', width: 100, align: "center", title: '状态', templet: '#statusTpl', unresize: true },
                { fixed: 'right', width: 120, title: '操作', toolbar: '#barTool' }
            ]
        ],
        done: function(res, curr, count){}, //表格数据渲染完的回调
    };
    // 搜索
    options.searchParams = function(){
        var formData = $.table._searchFormData();
        var data = {
            search_field: 'title',
            keyword: $('input[name="keyword"]').val(),
            filter_time: 'inputtime',
            filter_time_range: $("#laydate").val()
        };
        data = $.extend(formData, data);
        return data;
    };
    // 单元格编辑
    options.editTable = function(field, value, data) {
        // 参数说明：  field: 当前编辑的字段名, value: 得到修改后的值, data: 所在行的所有相关数据
        // ===排序操作===
        if(field == 'listorder') {
            $.post('{:url("cms/cms/listorder",["catid"=>$catid])}', {'id': data.id,'value':value }, function(data) {
                if (data.code == 1) {
                    layer.msg(data.msg);
                }else{
                    layer.msg(data.msg);
                }
            })
        }
    };
    // 头工具栏事件
    options.editToolbar = function(obj) {
        var table = layui.table;
        var checkStatus = table.checkStatus(obj.config.id),

            ids = [],
            id = tag = '';
        if (obj.event === 'move') {
            var data = checkStatus.data;
            if (data.length > 0) {
                for (let i in data) {
                    id += tag + data[i].id;
                    tag = '|';
                    //ids.push(data[i].id);
                }
                layer.open({
                    title: false,
                    type: 1,
                    content: $('#remove'),
                    area: ['300px', '250px'],
                    btn: ['移动'],
                    yes: function(index, layero){
                        var tocatid = $("select[name='remove']").val();
                        if (tocatid == 0) {
                            layer.msg("请选择移动的栏目");
                            return;
                        }
                        $.post('{:url("cms/cms/remove",["catid"=>$catid])}', { 'ids': id,'tocatid':tocatid}, function(data) {
                            if (data.code == 1) {
                                layer.msg(data.msg);
                                layer.close(index);
                            }else{
                                layer.msg(data.msg);
                            }
                        })
                    }

                });
            } else {
                layer.msg("请选择需要移动的数据");
            }

        }
    };
    // 加载layui表格
    $.table.init(options);

    // laydate控件
    $.layui.laydate({
        elem: '#laydate',
        range: true,
    });
</script>
{/block}